package com.lottery.game.runner.v2.common;

import java.util.Map;
import java.util.Set;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import com.lottery.game.runner.v2.RoomCoreExecutor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class RoomCoreExecutorContextHolder {

	private static Lock lock = new ReentrantLock();

	private static final Map<String, RoomCoreExecutor> CONTEXT_HOLDER = new ConcurrentHashMap<>();

	public static Vector<String> keys = new Vector<>();

	public static void put(String key, RoomCoreExecutor exec) {
		lock.lock();
		try {
			CONTEXT_HOLDER.put(key, exec);
			keys.add(key);
		} catch (Exception e) {
			 log.error("Put executor service into context, error: {}", e.getMessage(), e);
		} finally {
			lock.unlock();
		}
	}

	public static Boolean contains(String key) {
		return keys.contains(key);
	}

	public static ExecutorService remove(String key) {
		lock.lock();
		ExecutorService exec = null;
		try {
			//exec = CONTEXT_HOLDER.remove(key);
			keys.remove(key);
		} catch (Exception e) {
			 log.error("Remove executor service into context, error: {}", e.getMessage(), e);
		} finally {
			lock.unlock();
		}
		return exec;
	}

	public static RoomCoreExecutor getThreadByKey(String key) {
		if(contains(key))
			return CONTEXT_HOLDER.get(key);
		return null;
	}

	public static Set<String> getThreadByKey() {
		return CONTEXT_HOLDER.keySet();
	}

}
